package com.example.amazlinglistview.domain;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Pair;

public class CarInfo {

	private String									msgId				= "";
	private String									name				= "";
	private String									subSeries			= "";
	private String									priceByVendor		= "";
	private String									priceByDealer		= "";
	private String									engine				= "";
	private String									structure			= "";
	private String									transmission		= "";
	private String									image				= "";
	private String									vendor				= "";
	private String									level				= "";
	private String									lengthWidthHeight	= "";
	private String									engineDes			= "";
	private String									transmissionDes		= "";
	private String									driverManner		= "";
	private String									yearStyle			= "";
	private int										dealerNum			= 0;
	private List<DealerInfo>						dealerInfos;
	// 这个是用在Section中的
	private List<Pair<String, List<DealerInfo>>>	dealerInfosForSection;
	private String									imageNum			= "0";
	private String									subSeriesId			= "";

	public String getYearStyle() {
		return yearStyle;
	}

	public void setYearStyle(String yearStyle) {
		this.yearStyle = yearStyle;
	}

	public String getImageNum() {
		return imageNum;
	}

	public void setImageNum(String imageNum) {
		this.imageNum = imageNum;
	}

	public String getMsgId() {
		return msgId;
	}

	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSubSeries() {
		return subSeries;
	}

	public void setSubSeries(String subSeries) {
		this.subSeries = subSeries;
	}

	public String getPriceByVendor() {
		return priceByVendor;
	}

	public void setPriceByVendor(String priceByVendor) {
		this.priceByVendor = priceByVendor;
	}

	public String getPriceByDealer() {
		return priceByDealer;
	}

	public void setPriceByDealer(String priceByDealer) {
		this.priceByDealer = priceByDealer;
	}

	public String getEngine() {
		return engine;
	}

	public void setEngine(String engine) {
		this.engine = engine;
	}

	public String getStructure() {
		return structure;
	}

	public void setStructure(String structure) {
		this.structure = structure;
	}

	public String getTransmission() {
		return transmission;
	}

	public void setTransmission(String transmission) {
		this.transmission = transmission;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getVendor() {
		return vendor;
	}

	public void setVendor(String vendor) {
		this.vendor = vendor;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getLengthWidthHeight() {
		return lengthWidthHeight;
	}

	public void setLengthWidthHeight(String lengthWidthHeight) {
		this.lengthWidthHeight = lengthWidthHeight;
	}

	public String getEngineDes() {
		return engineDes;
	}

	public void setEngineDes(String engineDes) {
		this.engineDes = engineDes;
	}

	public String getTransmissionDes() {
		return transmissionDes;
	}

	public void setTransmissionDes(String transmissionDes) {
		this.transmissionDes = transmissionDes;
	}

	public String getDriverManner() {
		return driverManner;
	}

	public void setDriverManner(String driverManner) {
		this.driverManner = driverManner;
	}

	public int getDealerNum() {
		return dealerNum;
	}

	public void setDealerNum(int dealerNum) {
		this.dealerNum = dealerNum;
	}

	public List<DealerInfo> getDealerInfos() {
		if (null == dealerInfos) {
			dealerInfos = new ArrayList<DealerInfo>();
			dealerInfos.add(new DealerInfo());
		}
		return dealerInfos;
	}

	public void setDealerInfos(List<DealerInfo> dealerInfos) {
		this.dealerInfos = dealerInfos;
	}

	public String getSubSeriesId() {
		return subSeriesId;
	}

	public void setSubSeriesId(String subSeriesId) {
		this.subSeriesId = subSeriesId;
	}

	public List<Pair<String, List<DealerInfo>>> getDealerInfosForSection() {
		return dealerInfosForSection;
	}

	/** 解析JSON 生成车型的数据 */
	public boolean initCarInfoDataFromJson(String json) {

		try {
			JSONObject carInfoObject = new JSONObject(json);
			setMsgId(carInfoObject.isNull("msgId") == false ? carInfoObject.getString("msgId") : "");
			setEngine(carInfoObject.isNull("engine") == false ? carInfoObject.getString("engine") : "");
			setImage(carInfoObject.isNull("image") == false ? carInfoObject.getString("image") : "");
			setImageNum(carInfoObject.isNull("imageNum") == false ? carInfoObject.getString("imageNum") : "0");
			setName(carInfoObject.isNull("name") == false ? carInfoObject.getString("name") : "");
			setPriceByDealer(carInfoObject.isNull("priceByDealer") == false ? carInfoObject.getString("priceByDealer") : "");
			setPriceByVendor(carInfoObject.isNull("priceByVendor") == false ? carInfoObject.getString("priceByVendor") : "");
			setStructure(carInfoObject.isNull("structure") == false ? carInfoObject.getString("structure") : "");
			setDriverManner(carInfoObject.isNull("driverManner") == false ? carInfoObject.getString("driverManner") : "");
			setTransmission(carInfoObject.isNull("transmission") == false ? carInfoObject.getString("transmission") : "");
			setEngineDes(carInfoObject.isNull("engineDes") == false ? carInfoObject.getString("engineDes") : "");
			setLengthWidthHeight(carInfoObject.isNull("lengthWidthHeight") == false ? carInfoObject.getString("lengthWidthHeight") : "");
			setLevel(carInfoObject.isNull("level") == false ? carInfoObject.getString("level") : "");
			setSubSeries(carInfoObject.isNull("subSeries") == false ? carInfoObject.getString("subSeries") : "");
			setSubSeriesId(carInfoObject.isNull("subSeriesId") == false ? carInfoObject.getString("subSeriesId") : "");
			setYearStyle(carInfoObject.isNull("yearStyle") == false ? carInfoObject.getString("yearStyle") : "");
			setTransmissionDes(carInfoObject.isNull("transmissionDes") == false ? carInfoObject.getString("transmissionDes") : "");
			setVendor(carInfoObject.isNull("vendor") == false ? carInfoObject.getString("vendor") : "");
			// setDealerNum(carInfoObject.isNull("dealers") == false ?
			// carInfoObject.getJSONArray("dealers").length() : 0);

			// dealerInfosForSection = new ArrayList<Pair<String,
			// List<DealerInfo>>>();
			// Pair<String, List<DealerInfo>> pair_letter = new Pair<String,
			// List<DealerInfo>>("", dealerInfos);
			// dealerInfosForSection.add(pair_letter);

			// if (getDealerNum() > 0) {
			// for (int i = 0; i <
			// carInfoObject.getJSONArray("dealers").length(); i++) {
			// JSONObject dealerInfoObject =
			// carInfoObject.getJSONArray("dealers").getJSONObject(i);
			// DealerInfo dealerInfoItem = new DealerInfo();
			// dealerInfoItem.setId(dealerInfoObject.isNull("id") == false ?
			// dealerInfoObject.getString("id") : "");
			// dealerInfoItem.setPriceId(dealerInfoObject.isNull("priceId") ==
			// false ? dealerInfoObject.getString("priceId") : "");
			// dealerInfoItem.setPrice(dealerInfoObject.isNull("price") == false
			// ? dealerInfoObject.getString("price") : "");
			// dealerInfoItem.setName(dealerInfoObject.isNull("name") == false ?
			// dealerInfoObject.getString("name") : "");
			// dealerInfoItem.setType(dealerInfoObject.isNull("type") == false ?
			// dealerInfoObject.getString("type") : "");
			// dealerInfoItem.setPriceId(dealerInfoObject.isNull("priceId") ==
			// false ? dealerInfoObject.getString("priceId") : "");
			// dealerInfoItem.setAddress(dealerInfoObject.isNull("address") ==
			// false ? dealerInfoObject.getString("address") : "");
			// dealerInfoItem.setTelephone(dealerInfoObject.isNull("telephone")
			// == false ? dealerInfoObject.getString("telephone") : "");
			// dealerInfoItem.setLatitude(dealerInfoObject.isNull("latitude") ==
			// false ? dealerInfoObject.getString("latitude") : "");
			// dealerInfoItem.setLongitude(dealerInfoObject.isNull("longitude")
			// == false ? dealerInfoObject.getString("longitude") : "");
			// dealerInfos.add(dealerInfoItem);
			// }
			// }
			// setDealerInfos(dealerInfos);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/** 初始化车型对应的经销商的 */
	public boolean initCarDealerInfoDataFromJson(String json) {
		try {
			JSONObject dealersObject = new JSONObject(json);
			List<DealerInfo> dealerInfos = new ArrayList<DealerInfo>();
			for (int i = 0; i < dealersObject.getJSONArray("dealers").length(); i++) {
				JSONObject dealersObj = dealersObject.getJSONArray("dealers").getJSONObject(i);
				DealerInfo dealerInfoItem = new DealerInfo();
				dealerInfoItem.setId(dealersObj.isNull("id") ? "" : dealersObj.getString("id"));
				dealerInfoItem.setPriceId(dealersObj.isNull("priceId") ? "" : dealersObj.getString("priceId"));
				dealerInfoItem.setName(dealersObj.isNull("name") ? "" : dealersObj.getString("name"));
				dealerInfoItem.setFullName(dealersObj.isNull("fullname") ? "" : dealersObj.getString("fullname"));
				dealerInfoItem.setLongitude(dealersObj.isNull("longitude") ? "" : dealersObj.getString("longitude"));
				dealerInfoItem.setLatitude(dealersObj.isNull("latitude") ? "" : dealersObj.getString("latitude"));
				dealerInfoItem.setTitle(dealersObj.isNull("title") ? "" : dealersObj.getString("title"));
				dealerInfoItem.setUrl(dealersObj.isNull("url") ? "" : dealersObj.getString("url"));
				dealerInfoItem.setType(dealersObj.isNull("type") ? "" : dealersObj.getString("type"));
				dealerInfoItem.setPrice(dealersObj.isNull("price") ? "" : dealersObj.getString("price"));
				dealerInfoItem.setAddress(dealersObj.isNull("address") ? "" : dealersObj.getString("address"));
				dealerInfoItem.setTelephone(dealersObj.isNull("telephone") ? "" : dealersObj.getString("telephone"));
				dealerInfoItem.setPermission((Integer) (dealersObj.isNull("premission") ? "" : dealersObj.getInt("premission")));

				dealerInfos.add(dealerInfoItem);
			}
			setDealerInfos(dealerInfos);
			return true;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return false;
	}
}
